import { createRouter, createWebHashHistory } from 'vue-router'
/**
 * 1.导入组件
 * 2.在routes数组中配置：{path:'path',component:'页面组件'}
 */
// 所有页面骨架
// import Layout from '@/views/Layout/index.vue'
// 默认加载原理：默认加载的是一个函数 => 什么时候调用什么时候执行（访问路由）
const Layout = () => import('@/views/Layout')
// 导入页面
// import Home from '@/views/Home/index.vue'
const Home = () => import('@/views/Home')
// 一级分类
const Category = () => import('@/views/Category')
const CategorySubtitle = () => import('@/views/Category/sub.vue')
// 页面详情
const Goods = () => import('@/views/Goods/index')

const routes = [
  {
    path: '/',
    component: Layout,
    children: [{
      // 首页
      path: '/',
      component: Home
    }, {
      // 一级分类
      path: '/category/:id',
      component: Category
    }, {
      path: '/category/sub/:id',
      component: CategorySubtitle
    }, {

      path: '/goods/:id',
      component: Goods

    }]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  /**
   * 问题：路由切换辉停留上个页面切换的滚动位置
   * 解决：创建路由时，加入scrollBehavior方法
   * v2写法：scrollBehavior方法return {x:0,y:0}
   * v3写法：scrollBehavior方法return {left:0,top:0}
   * @returns
   */
  scrollBehavior () {
    return { top: 0, left: 0 }
  }
})

export default router
